import 'dart:convert';

import 'package:flutter/material.dart';
import '../config/api.dart';

class GoodsDetal extends StatefulWidget {
  Map arguments;
  //int id;
  GoodsDetal({Key key, this.arguments}) : super(key: key);

  @override
  _GoodsDetalState createState() => _GoodsDetalState(arguments: this.arguments);
}

class _GoodsDetalState extends State<GoodsDetal>
    with SingleTickerProviderStateMixin {
  Map arguments;
  //int id;
  Map<String, dynamic> goods = {
    "message": [
      {
        "url": 'http://dbyxs.xyz:3005/uploads/b593883c3adac89564d762ae7b76ad5e',
        "goodsname": "请稍后数据加载中",
        "price": "",
        "maxpeople":""
      }
    ]
  };

  @override
  void initState() {
    super.initState();
    this.getgoods(arguments['id']);
    //  print('${arguments['id']}最新的值');
    //this.goodsget();
  }
bool flag=false;
  void getgoods(v) {
    // print('${v}没有变化吗');
    getlists(v).then((res) => {
          // print(JsonDecoder(res.data))
          setState(() {
            if (mounted) {
              this.goods = res.data;
              this.goodsget();
              this.flag=true;
            } else {
              //  this.goods=json.decode(res.data.toString());
            }
            //  print('${goods['message']}长度是多少');
          })
          //print('22${res.data['message']}类型数据')
        });
  }

  List<Widget> goodsGet() {
    List<Widget> list = new List();
    list.add(Center(
      child: Text('数据加载中',style: TextStyle(color:Colors.black38),),
    ));
  }

  List<Widget> goodsget() {
    List<Widget> list = new List();
    //  print('没有数据吗1${this.goods['message'][0]['url'].split(',')[0]}');
    //this.goods['message'].length>0 this.goods['message']!=[]
 //   print(this.goods);
    // var data=json.decode(goods.toString());
    if (this.goods['message'] != null) {
      for (var i = 0; i < this.goods['message'].length; i++) {
        //   print(i);
        list.add(InkWell(
          onTap: () {
            Navigator.of(context).pushNamed('/goodscommodity',arguments: {"id":this.goods['message'][i]});
           // Navigator.of(context).pushNamed('/goodscommodity',arguments: {"id":this.goods['message'][i]['id']});
  // this.goods['message'][i]['id']?'': Navigator.of(context).pushNamed('/goodscommodity',arguments: {"id":this.goods['message'][i]['id']});
          },
           child:
            Container(
              height: 200,
              width: 175,
           // flex: 1,
          child: ListView(
            
            physics:NeverScrollableScrollPhysics(),
            //'http://dbyxs.xyz:3005/uploads/b593883c3adac89564d762ae7b76ad5e'
            children: <Widget>[
              Image.network(
                this.goods['message'][i]['url'].split(',')[0],
                width: 150,
                height: 150,
                fit: BoxFit.cover,
              ),
              Text(
                '${this.goods['message'][i]['goodsname']}',
                overflow: TextOverflow.ellipsis,
                maxLines: 2,
              ),
              SizedBox(
                height: 5,
              ),
              Row(children: <Widget>[
                Text(this.goods['message'][i]['price']==''?'':'  ￥${this.goods['message'][i]['price']}.00元',
                    textAlign: TextAlign.start,
                    style: TextStyle(color: Colors.red)),
                SizedBox(
                  width: 10,
                ),
                Text(
                  this.goods['message'][i]['maxpeople']==''?'':'${this.goods['message'][i]['maxpeople']}付款',
                  textAlign: TextAlign.start,
                  style: TextStyle(color: Colors.grey),
                )
              ])
            ],
          )
           )
          ,
        ));
      }
    } else {
      for (var i = 0; i < 2; i++) {
        print(i);
        list.add(Text('加载中'));
      }
    }
    return list.toList();
  }
Widget _list(BuildContext context, int index){
  return  Container(
              height: 200,
              width: 175,
           // flex: 1,
          child: ListView(
            physics:NeverScrollableScrollPhysics(),
            //'http://dbyxs.xyz:3005/uploads/b593883c3adac89564d762ae7b76ad5e'
            children: <Widget>[
              Image.network(
                this.goods['message'][index]['url'].split(',')[0],
                width: 150,
                height: 150,
                fit: BoxFit.cover,
              ),
              Text(
                '${this.goods['message'][index]['goodsname']}',
                overflow: TextOverflow.ellipsis,
                maxLines: 2,
              ),
              SizedBox(
                height: 5,
              ),
              Row(children: <Widget>[
                Text(this.goods['message'][index]['price']==''?'':'  ￥${this.goods['message'][index]['price']}.00元',
                    textAlign: TextAlign.start,
                    style: TextStyle(color: Colors.red)),
                SizedBox(
                  width: 10,
                ),
                Text(
                  this.goods['message'][index]['maxpeople']==''?'':'${this.goods['message'][index]['maxpeople']}付款',
                  textAlign: TextAlign.start,
                  style: TextStyle(color: Colors.grey),
                )
              ])
            ],
          )
           );
}
  _GoodsDetalState({this.arguments});
  @override
  Widget build(BuildContext context) {
    print('${this.goods['message'].length}是空数组吗');
    // super.build(context);

    if(this.flag){
       return Scaffold(
        // body:ListView.builder(itemBuilder: _list,itemCount:this.goods['message'].length)
        //  appBar: AppBar(title:Text('我是详情页面${arguments['id']}')),
        body: GridView.count(
            primary: true,
            crossAxisCount: 2,
            mainAxisSpacing: 5.0,
            crossAxisSpacing: 5.0,
            //  physics: NeverScrollableScrollPhysics(),
            padding: EdgeInsets.all(10),
            shrinkWrap: true,
            // reverse :false,Axis.horizontal scrollDirection
            // scrollDirection: Axis.vertical,
            childAspectRatio: 5 / 6.5,
            // children: ,
          // children: <Widget>[
          //   ListView.builder(itemBuilder: _list,itemCount:this.goods['message'].length)
          // ],
            children: this.goods['message'][0]['maxpeople']=='' ? goodsGet(): goodsget()
            // <Widget>[
            //   ListView(children: <Widget>[
            //      Image.network('http://dbyxs.xyz:3005/uploads/b593883c3adac89564d762ae7b76ad5e',fit: BoxFit.cover,),
            //           Text('你好')
            //   ],
            //   )
            // ]
            )
        // Column(
        //   children:this.goodsget()
        // ),
        );
      
    }else{
     return Scaffold(
        body: Text('数据加载中'),
      );
    }
  }
}
class Pages extends StatelessWidget {
  final List swiper;
   Pages({Key key,this.swiper}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text('我誓死'),
    );
  }
}
